package net.zjitc.servlet.request;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Author Administrator
 * @Date 2020/5/15 11:06
 * 客户访问forwardServlet1,转发到另一个Servlet(forwardServlet2)
 **/
@WebServlet("/forwardServlet1")
public class ForwardServlet1 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("forwardServlet1.....");
        /* request域共享数据：
        *   请求转发（一次请求）之间共享数据*/
        /* 存储数据*/
        req.setAttribute("msg","haha");

        /* 请求转发: 服务器内部的一种资源跳转方式，由多个Servlet来共同处理一个请求*/
        RequestDispatcher requestDispatcher = req.getRequestDispatcher("/forwardServlet2");
        requestDispatcher.forward(req,resp);

        /* 转发只能是内部进行，无法转发到外部资源*/
//        req.getRequestDispatcher("http://baidu.com").forward(req,resp);


    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req,resp);
    }
}
